<!--
 * @Descripttion: 
 * @version: 
 * @Author: lsy
 * @Date: 2021-12-14 16:18:52
 * @LastEditors: lsy
 * @LastEditTime: 2021-12-14 17:15:43
-->
<template>
    <a-modal
        v-if="visible"
        v-model="visible"
        title="修改密码"
        ok-text="确认"
        cancel-text="取消"
        @ok="handleOk"
    >
        <a-form id="changPwd_form" :form="form" class="changPwd_form">
            <a-form-item>
                <a-input
                    v-decorator="[
                        'oldpwd',
                        {
                            rules: [{ required: true, message: '请输入原密码!' }],
                        },
                    ]"
                    type="password"
                    placeholder="原密码"
                >
                    <a-icon slot="prefix" type="lock" style="color: rgba(0, 0, 0, 0.25)" />
                </a-input>
            </a-form-item>
            <a-form-item>
                <a-input
                    v-decorator="[
                        'newpwd1',
                        {
                            rules: [
                                { required: true, message: '请输入新密码!' }
                            ],
                        },
                    ]"
                    type="password"
                    placeholder="新密码"
                >
                    <a-icon slot="prefix" type="lock" style="color: rgba(0, 0, 0, 0.25)" />
                </a-input>
            </a-form-item>
            <a-form-item>
                <a-input
                    v-decorator="[
                        'newpwd2',
                        {
                            rules: [
                                { required: true, message: '请重复输入新密码!' },
                                { validator: checkPwd }
                            ],
                        },
                    ]"
                    type="password"
                    placeholder="重复输入"
                >
                    <a-icon slot="prefix" type="lock" style="color: rgba(0, 0, 0, 0.25)" />
                </a-input>
            </a-form-item>
        </a-form>
    </a-modal>
</template>
<script>
import { JSEncrypt } from 'jsencrypt'
export default {
    name: "changePwd",
    data() {
        return {
            form: this.$form.createForm(this, { name: 'changPwd_form' }),
            visible: false,
        }
    },
    mounted() {
    },
    methods: {
        handleOk() {
            this.form.validateFields((err, values) => {
                if (!err) {
                    VE_API.system.getPublic_key({}, `?username=${values.username}`).then(res => {//获取public_key
                        let jsencrypt = new JSEncrypt();
                        jsencrypt.setPublicKey(res.data.data.publicKey);
                        let param = {
                            id: localStorage.getItem("userId"),
                            loginName: localStorage.getItem("UserName"),
                            loginPass: jsencrypt.encrypt(values.newpwd1),
                            oldLoginPass: jsencrypt.encrypt(values.oldpwd),
                            public_key: res.data.data.publicKey
                        }
                        VE_API.user.changePWD(param).then(res => {
                            this.$router.push("/login");
                            this.visible = false;
                        })
                    })
                }
            });
        },
        checkPwd(rule, value, callback) {
            if (this.form.getFieldsValue().newpwd1 == value) {
                callback();
                return;
            }
            callback('两次输入不一致');
        },
    }
}
</script>